package com.cai.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject rabbitMQ_SpringBoot
 * @Package com.cai.config
 * @Author Uli1024
 * @CreaterTime 2022/7/4 17:21
 */
@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE = "seckillExchange";
    public static final String QUEUE = "seckillQueue";

    public static final String DEAD_LETTER_EXCHANGE = "exchange_dlx";
    public static final String DEAD_LETTER_QUEUE = "queue_dlx";

    public static final String DEAD_LETTER_ROUTING_KEY = "dlx.seckill.fail";
    public static final String ROUTING_KEY = "seckill.message";

    /**
     * 配置死信交换器
     */
    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 配置死信队列
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    /**
     * 死信队列绑定
     *
     * @return
     */
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(DEAD_LETTER_ROUTING_KEY);
    }

    /**
     * 业务交换器
     *
     * @return
     */
    @Bean
    public TopicExchange businessExchange() {
        return new TopicExchange(EXCHANGE);
    }

    /**
     * 业务队列
     *
     * @return
     */
    @Bean
    public Queue businessQueue() {
        Map<String, Object> args = new HashMap<>(2);
        // 配置当前队列绑定的死信交换器
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // 配置当前队列的死信队列路由key，如果不设置默认为当前队列的路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(QUEUE).withArguments(args).build();
    }

    /**
     * 业务队列绑定
     *
     * @return
     */
    @Bean
    public Binding queueBinding() {
        return BindingBuilder.bind(businessQueue())
                .to(businessExchange())
                .with(ROUTING_KEY);
    }


    //@Autowired
    //private MQReceiver mqReceiver;
    //
    //@Bean
    //public RabbitTemplate rabbitTemplate() {
    //    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    //
    //    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    //    //exchange创建
    //    TopicExchange topicExchange = new TopicExchange(EXCHANGE);
    //    rabbitAdmin.declareExchange(topicExchange);
    //
    //    //创建队列
    //    Map<String, Object> map = new HashMap<>();
    //    //正常队列设置队列过期时间
    //    map.put("x-message-ttl", 10000);
    //    //正常队列绑定死信交换机
    //    map.put("x-dead-letter-exchange", "exchange_dlx");
    //    //正常队列发送给死信交换机的routingKey
    //    map.put("x-dead-letter-routing-key", "dlx.seckill.fail");
    //
    //    Queue queue = new Queue(QUEUE, true, false, false, null);
    //    rabbitAdmin.declareQueue(queue);
    //
    //    //绑定交换机和队列
    //    rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(topicExchange).with("seckill.message"));
    //
    //    //创建监听容器
    //    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    //    container.addQueueNames(QUEUE);
    //    container.setMessageListener(mqReceiver);
    //
    //    //设置确认模式为手工确认
    //    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    //    container.start();
    //
    //
    //    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    //    return rabbitTemplate;
    //}
}
